package work.chenbo.spring_boot_shiro.common.util;

import org.apache.commons.codec.binary.Base64;

import java.io.UnsupportedEncodingException;


/**
 * 编解码工具类
 * @author ChenBo
 * @className CodecUtils
 * @date 2019/10/29
 */

public class CodecUtils {

    private static final String CHARSET_NAME = "UTF-8";

    /**
     * Base64 编码 默认(UTF8)
     * @author ChenBo
     * @date 2019/10/29
     */
    public static String encodeBase64(String str) {
        try {
            byte[] b = new Base64().encode(str.getBytes(CHARSET_NAME));
            return new String(b, CHARSET_NAME);
        } catch (UnsupportedEncodingException e) {
           e.printStackTrace();
        }
        return "";
    }

    /**
     * Base64 解码 默认(UTF8)
     * @author ChenBo
     * @date 2019/10/29
     */
    public static String decodeBase64(String str) {
        try {
            byte[] b = new Base64().decode(str.getBytes(CHARSET_NAME));
            return new String(b, CHARSET_NAME);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return "";
    }
}
